
function twoSum(nums: number[], target: number): number[] {
    const xs = nums.map((v, i) => ({ index: i, value: v})).sort((x, y) => x.value - y.value);
    let head = 0;
    let tail = xs.length - 1;
    while(head < xs.length && tail > head) {
        let sum = xs[head].value + xs[tail].value;
        if (sum === target) return [ xs[head].index, xs[tail].index ];
        else if (sum <= target) head++;
        else tail--;
    }
};

console.assert(twoSum( [2, 7, 11, 15], 9).join(',') === '0,1')
console.assert(twoSum( [3,2,4], 6).join(',') === '1,2')

console.log("success !");